package com.xin.mmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xin.mmall.entity.User;
import com.xin.mmall.entity.UserAddress;
import com.xin.mmall.mapper.UserAddressMapper;
import com.xin.mmall.service.CartService;
import com.xin.mmall.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴玉鑫
 * @since 2021-05-13
 */
@Controller
@RequestMapping("/userAddress")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;
    @Autowired
    private CartService cartService;


    @GetMapping("/list")
    public ModelAndView list(HttpSession session){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("userAddressList");
        HashMap<String, Object> map = new HashMap<>();
        User user = (User) session.getAttribute("user");
        map.put("user_id",user.getId());
        List<UserAddress> userAddresses = userAddressService.listByMap(map);
        modelAndView.addObject("cartList",cartService.findAllCartVOByUserId(user.getId()));
        modelAndView.addObject("userAddress",userAddresses);
        return modelAndView;
    }
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        String ok = id+"->delete";
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        boolean remove = userAddressService.remove(wrapper);
        return "redirect:/userAddress/list";
    }
    @GetMapping("/update/{id}")
    public ModelAndView update(@PathVariable("id") Integer id){
        String ok = id+"->update";
        UserAddress userAddress = userAddressService.getById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("update");
        modelAndView.addObject("userAddress",userAddress);
        System.out.println(ok);
        return modelAndView;
    }
    @PostMapping("/save")
    public String save(HttpServletRequest request){
        Integer id = Integer.valueOf(request.getParameter("id"));
        String address = request.getParameter("userAddress");
        String remark = request.getParameter("remark");
        Integer isdefault = Integer.valueOf(request.getParameter("isdefault"));

        UpdateWrapper<UserAddress> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id)
                .set("address", address)
                .set("remark",remark)
                .set("isdefault",isdefault);
        userAddressService.update(null, updateWrapper);
        return "redirect:/userAddress/list";
    }
}

